﻿using KAs.Cache;
using Microsoft.Extensions.Configuration;

namespace Microsoft.Extensions.DependencyInjection;

// 为什么要使用分布式缓存：基于微服务框架需求考虑，分布式缓存有利于不同服务间的串联
public static class CacheExtensions
{
    /// <summary>
    /// 基于IDistributedCache,添加Redis缓存服务
    /// </summary>
    /// <param name="services"></param>
    /// <param name="configuration"></param>
    public static void AddRedisDistributedCacheService(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddDistributedMemoryCache();
        services.AddStackExchangeRedisCache(options =>
        {
            options.Configuration = configuration["Redis:ConnectionString"];
            options.InstanceName = configuration["Redis:InstanceName"];
        });
        services.AddSingleton<ICache, DistributedCache>();
    }
    // - -没条件或简单程序也没必要 直接用内存缓存把
    public static void AddMemoryCacheService(this IServiceCollection services)
    {
        services.AddMemoryCache();
        services.AddSingleton<ICache, MemoryCache>();
    }
}